package qs.crm.modules.client.cstactity.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import qs.crm.annotation.Log;
import qs.crm.modules.client.cstactity.domain.Cstactity;
import qs.crm.modules.client.cstactity.service.CstactityService;
import qs.crm.modules.client.cstactity.service.dto.CstactityQueryCriteria;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @website https://eladmin.vip
* @author tong
* @date 2023-06-01
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "客户交往记录管理")
@RequestMapping("/api/cstactity")
public class CstactityController {

    private final CstactityService cstactityService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('cstactity:list')")
    public void exportCstactity(HttpServletResponse response, CstactityQueryCriteria criteria) throws IOException {
        cstactityService.download(cstactityService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询客户交往记录")
    @ApiOperation("查询客户交往记录")
    @PreAuthorize("@el.check('cstactity:list')")
    public ResponseEntity<Object> queryCstactity(CstactityQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(cstactityService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增客户交往记录")
    @ApiOperation("新增客户交往记录")
    @PreAuthorize("@el.check('cstactity:add')")
    public ResponseEntity<Object> createCstactity(@Validated @RequestBody Cstactity resources){
        return new ResponseEntity<>(cstactityService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改客户交往记录")
    @ApiOperation("修改客户交往记录")
    @PreAuthorize("@el.check('cstactity:edit')")
    public ResponseEntity<Object> updateCstactity(@Validated @RequestBody Cstactity resources){
        cstactityService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除客户交往记录")
    @ApiOperation("删除客户交往记录")
    @PreAuthorize("@el.check('cstactity:del')")
    public ResponseEntity<Object> deleteCstactity(@RequestBody Long[] ids) {
        cstactityService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}